<template>
  <div id="play" class="music_play_page">
    <div class="music_cover">
      <div class="hide_button"
        @click="hidePlayPage"
        @touchend.prevent.stop="hidePlayPage">
        <img src="/static/images/icon-jiantou.png">
      </div>
      <img v-lazy="coverImgUrl"
        @touchstart.prevent="movestart"
        @touchend.prevent="moveend">
    </div>

    <div class="play_control">
      <img :src="coverImgUrl" class="blurbg">
      <div class="progress_bar_group">
        <div class="progress_bar">
          <div class="progress" :style="{ width: indicatorPosition+'%' }"></div>
          <div class="indicater" :style="{ left: indicatorPosition+'%' }"></div>
        </div>
        <div class="time_indicater">
          <span>{{ currentTime }}</span>
          <span>{{ duration }}</span>
        </div>
      </div>
      <div class="music_info">
        <p class="music_name">{{ song.name }}</p>
        <p class="music_author">{{ song.singer | singer }}</p>
      </div>
      <div class="lyric">
        <lyric :songid="song.id" :currentTime="currentTime"></lyric>
      </div>
      <div class="button_group">
        <ul>
          <li><img src="/static/images/icon-like.png" alt="like"></li>
          <li><img src="/static/images/icon-shangyiqu.png" alt="prev"
                    @click.prevent="playFront"
                    @touchend.prevent="playFront"></li>
          <li><img :src="playing? $parent.iconPause: $parent.iconPlay" alt="play"
                    @click.prevent="$parent.playingToggle"
                    @touchend.prevent="$parent.playingToggle"></li>
          <li><img src="/static/images/icon-xiayiqu.png" alt="next"
                    @click.prevent="playNext"
                    @touchend.prevent="playNext"></li>
          <li><img src="/static/images/icon-list.png" alt="list"
                    @click.prevent="showPlayingList"
                    @touchend.prevent="showPlayingList"></li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import Lyric from './lyric'
import { mapState, mapMutations, mapGetters } from 'vuex'

export default {
  components: {
    Lyric
  },
  name: 'playpage',
  data() {
    return {
      clientY: 0
    }
  },
  methods: {
    hidePlayPage() {
      this.$parent.playPageShow = false
    },
    movestart(e) {
      this.clientY = e.touches[0].clientY
    },
    moveend(e) {
      if(e.changedTouches[0].clientY - this.clientY > 0) 
        this.hidePlayPage()
    },
    showPlayingList() {
      this.$store.commit('showPlayingList')
    },
    ...mapMutations([ 'playNext', 'playFront' ])
  },
  computed: {
    ...mapGetters([ 'coverImgUrl', 'currentTime', 'duration' ]),
    ...mapState({
      song: state => state.PlayService.song,
      playing: state => state.PlayService.playing,
      indicatorPosition: state => state.PlayService.currentTime / state.PlayService.duration * 100
    })
  },
  filters: {
    singer: val => {
      if(typeof val === 'string') {
        return val
      } else if (val instanceof Array) {
        let singer = ''
        val.forEach(item => {
          singer = singer + item.name + ' '
        })
        return singer
      }
    }
  }
}
</script>

<style lang="less" scoped>
  * {
    margin: 0;
    padding: 0;
  }
  .music_play_page {
    width: 100%;
    max-width: 68vh;
    height: 100%;
    position: fixed;
    top: 40px;
    z-index: 2;
    .music_cover {
      width: 100%;
      height: 100vw;
      max-height: calc(68vh - 90px);
      max-height: -webkit-calc(68vh - 90px);
      max-height: -moz-calc(68vh - 90px);
      max-height: -o-calc(68vh - 90px);
      position: relative;
      img {
        width: 100%;
        height: 100%;
        display: block;
        -webkit-user-drag: none;
      }
      .hide_button {
        width: 25px;
        height: 25px;
        border-radius: 50%;
        background: rgba(255, 255, 255, 0.8);
        position: absolute;
        top: 27px;
        left: 11px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        img {
          width: 12px;
          height: 12px;
        }
      }
    }
    .play_control {
      width: 100%;
      background: rgba(255, 255, 255, .76);
      position: absolute;
      top: calc(68vh - 90px);
      top: -webkit-calc(68vh - 90px);
      top: -moz-calc(68vh - 90px);
      top: -o-calc(68vh - 90px);
      bottom: 90px;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      overflow: hidden;
      .blurbg {
        width: 100%;
        position: absolute;
        z-index: -1;
        -webkit-filter: blur(30px);
        -moz-filter: blur(30px);
        -ms-filter: blur(30px);
        -o-filter: blur(30px);
        filter: blur(30px);
      }
      .progress_bar_group {
        height: 30px;
        .progress_bar {
          height: 4px;
          background: #ccc;
          position: relative;
          .progress {
            height: 100%;
            background: #7f7f77;
          }
          .indicater {
            width: 2px;
            height: 12px;
            background: #ff2d55;
            position: absolute;
            top: 0;
          }
        }
        .time_indicater {
          margin-top: 8px;
          display: flex;
          justify-content: space-between;
          padding: 0 10px;
          span {
            font-size: 12px;
            line-height: 16px;
          }
        }
      }
      .music_info {
        text-align: center;
        .music_name {
          font-size: 18px;
          font-weight: bold;
          color: #000;
        }
        .music_author {
          font-size: 12px;
          color: #8f8f8f;
        }
      }
      .lyric {
        min-height: 35px;
        flex: 2;
        display: flex;
        flex-direction: column;
      }
      .button_group {
        height: 50px;
        ul {
          padding: 0 10px;
          list-style: none;
          display: flex;
          justify-content: space-between;
          align-items: center;
          li {
            height: 100%;
            cursor: pointer;
            img {
              width: 35px;
            }
          }
        }
      }
    }
  }
</style>
